package com.poulpicious.ethanj.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ResourceLoader {
	
	private static Map<String, String> imagesRef = new HashMap<String, String>();
	private static Map<String, String> soundsRef = new HashMap<String, String>();
	
	private static boolean finished;
	
	private static String state = "";
	private static int loaded;
	private static float percentage;
	
	private static int size;
	
	public static void preloadImage(String s, String name) {
		s = "res/images/" + s;
		
		File f = new File(s);

		if(f.exists() && s.contains(".png"))
			imagesRef.put(name, s);
		
		size++;
	}
	
	public static void preloadSound(String s, String name) {
		s = "res/sounds/" + s;
		
		File f = new File(s);
		
		if(f.exists())
			soundsRef.put(name, s);
		
		System.out.println(soundsRef.size());
		size++;
	}
	
	public static void loadNextResource() {
		if(imagesRef.size() > 0) {
			state = "Loading images...";
			String s = imagesRef.keySet().iterator().next();
			ResourceManager.loadImage(s, imagesRef.get(s));
			imagesRef.keySet().remove(s);
			loaded++;
		} else {
			if(soundsRef.size() > 0) {
				state = "Loading sounds...";
				loaded++;
				String s = soundsRef.keySet().iterator().next();
				ResourceManager.loadSound(s, soundsRef.get(s));
				soundsRef.keySet().remove(s);
				System.out.println(soundsRef.size());
			} else {
				setFinished(true);
			}
		}
		if (!finished) {
			percentage = ((loaded / (float)getSize()));
		}
	}
	
	private static int getSize() {
		return size;
	}

	public static boolean isFinished() {
		return finished;
	}

	public static void setFinished(boolean finished) {
		ResourceLoader.finished = finished;
	}

	public static String getState() {
		return state;
	}

	public static float getPercentage() {
		return percentage;
	}

	public static boolean hasNext() {
		return size > 0;
	}

}
